package com.chookss.home.audit.adapter;

import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.chookss.R;
import com.chookss.Urls;
import com.chookss.home.audit.clockIn.ClockInAskHolidayDetailActivity;
import com.chookss.home.audit.clockIn.ClockInReissueCardActivity;
import com.chookss.home.audit.clockIn.ClockInReissueCardDetailActivity;
import com.chookss.home.audit.clockIn.ClockInUtils;
import com.chookss.home.entity.ClockInRecordEntity;
import com.chookss.tools.Utils;

import java.util.List;

public class ClockInRecordAdapter extends BaseQuickAdapter<ClockInRecordEntity, BaseViewHolder> {
    private Activity context;
    private int tag = 0;//0个人考勤记录 1项目管理查询单人考勤记录
    private String groupCode = "";

    public ClockInRecordAdapter(int layoutResId, Activity context, @io.reactivex.annotations.Nullable List<ClockInRecordEntity> data) {
        super(layoutResId, data);
        this.context = context;
    }

    public void setGroupCode(String groupCode) {
        this.groupCode = groupCode;
    }

    @Override
    protected void convert(BaseViewHolder helper, ClockInRecordEntity entity) {
        TextView tvTime = helper.getView(R.id.tvTime);
        TextView tvAmRule = helper.getView(R.id.tvAmRule);
        TextView tvPmRule = helper.getView(R.id.tvPmRule);
        TextView tvTimeType = helper.getView(R.id.tvTimeType);
        TextView tvAmTime = helper.getView(R.id.tvAmTime);
        TextView tvAmGroup = helper.getView(R.id.tvAmGroup);
        TextView tvAmType = helper.getView(R.id.tvAmType);
        TextView tvAmLoc = helper.getView(R.id.tvAmLoc);
        TextView tvPmTime = helper.getView(R.id.tvPmTime);
        TextView tvPmGroup = helper.getView(R.id.tvPmGroup);
        TextView tvPmType = helper.getView(R.id.tvPmType);
        TextView tvPmLoc = helper.getView(R.id.tvPmLoc);
        TextView tvAmWaiqin = helper.getView(R.id.tvAmWaiqin);
        TextView tvPmWaiqin = helper.getView(R.id.tvPmWaiqin);
        View line = helper.getView(R.id.line);
        LinearLayout llAm = helper.getView(R.id.llAm);
        LinearLayout llPm = helper.getView(R.id.llPm);
        if (Utils.isNull(groupCode)) {
            //正常情况
            llAm.setVisibility(View.VISIBLE);
            llPm.setVisibility(View.VISIBLE);
            line.setVisibility(View.VISIBLE);
        } else {
            //项目管理下查询个人考勤记录
            if (Utils.isNull(entity.getItemGroupCode())) {
                llAm.setVisibility(View.GONE);
            } else {
                if (groupCode.equals(entity.getItemGroupCode())) {
                    llAm.setVisibility(View.VISIBLE);
                } else {
                    llAm.setVisibility(View.GONE);
                }
            }
            if (Utils.isNull(entity.getXiabanItemGroupCode())) {
                llPm.setVisibility(View.GONE);
            } else {
                if (groupCode.equals(entity.getXiabanItemGroupCode())) {
                    llPm.setVisibility(View.VISIBLE);
                } else {
                    llPm.setVisibility(View.GONE);
                }
            }

            if (llAm.getVisibility() == View.VISIBLE && llPm.getVisibility() == View.VISIBLE) {
                line.setVisibility(View.VISIBLE);
            } else {
                line.setVisibility(View.GONE);
            }
        }
        if (Utils.isNull(groupCode)) {
            tvTime.setText(entity.getStatisDate() + " " + ClockInUtils.getWeek(entity.getStatisDate()));
        } else {
            tvTime.setText(Utils.getAccurateDate2(entity.getCreateTime(), 0, 10) + " " + ClockInUtils.getWeek(Utils.getAccurateDate2(entity.getCreateTime(), 0, 10)));
        }
        if (Utils.isNull(entity.getHolidayName())) {
            tvTimeType.setVisibility(View.GONE);
        } else {
            tvTimeType.setText(entity.getHolidayName());
            tvTimeType.setVisibility(View.VISIBLE);
        }
        if (Utils.isNull(entity.getRuleStartTime())) {
            tvAmRule.setText("(要求时间: 09:00)");
        } else {
            tvAmRule.setText("(要求时间: " + entity.getRuleStartTime() + ")");
        }
        if (Utils.isNull(entity.getRuleEndTime())) {
            tvPmRule.setText("(要求时间: 18:00)");
        } else {
            tvPmRule.setText("(要求时间: " + entity.getRuleEndTime() + ")");
        }
        if (Utils.isNull(entity.getShangbanDakaType()) && Utils.isNull(entity.getShangbanBukaAuditCd())) {
            tvAmTime.setText("");
            tvAmType.setText("未打卡");
            tvAmLoc.setText("未知");
            tvAmWaiqin.setVisibility(View.GONE);
            tvAmType.setVisibility(View.VISIBLE);
        } else {
            if ("1".equals(entity.getWaiqinCd())) {
                //外勤打卡
                tvAmTime.setText("上班时间: " + ClockInUtils.getMinute(entity.getStartTime()));
                tvAmType.setText(ClockInUtils.getClockInType(entity.getShangbanDakaType()));
                if ("1".equals(entity.getShangbanDakaType())) {
                    tvAmType.setVisibility(View.GONE);
                } else {
                    tvAmType.setVisibility(View.VISIBLE);
                }
                tvAmWaiqin.setVisibility(View.VISIBLE);
            } else {
                //不是外勤
                tvAmWaiqin.setVisibility(View.GONE);
                tvAmType.setVisibility(View.VISIBLE);
                if (!Utils.isNull(entity.getShangbanDakaType())) {
                    if (Utils.isNull(entity.getStartTime())) {
                        tvAmTime.setText("");
                    } else {
                        tvAmTime.setText("上班时间: " + ClockInUtils.getMinute(entity.getStartTime()));
                    }
                    tvAmType.setText(ClockInUtils.getClockInType(entity.getShangbanDakaType()));
                } else if (!Utils.isNull(entity.getShangbanBukaAuditCd())) {
                    tvAmTime.setText("上班时间: " + ClockInUtils.getMinute(entity.getBukaStartTime()));
                    tvAmType.setText(ClockInUtils.getClockInReissueCard(entity.getShangbanBukaAuditCd()));
                }
            }

            if (Utils.isNull(entity.getDakaAddress())) {
                tvAmLoc.setText("未知");
            } else {
                tvAmLoc.setText(entity.getDakaAddress());
            }
        }
        if (Utils.isNull(entity.getXiabanDakaType()) && Utils.isNull(entity.getXiabanBukaAuditCd())) {
            tvPmTime.setText("");
            tvPmType.setText("未打卡");
            tvPmLoc.setText("未知");
            tvPmWaiqin.setVisibility(View.GONE);
            tvPmType.setVisibility(View.VISIBLE);
        } else {
            if ("1".equals(entity.getXiabanWaiqinCd())) {
                //外勤打卡
                tvPmTime.setText("下班时间: " + ClockInUtils.getMinute(entity.getEndTime()));
                tvPmType.setText(ClockInUtils.getClockInType(entity.getXiabanDakaType()));
                if ("1".equals(entity.getXiabanDakaType())) {
                    tvPmType.setVisibility(View.GONE);
                } else {
                    tvPmType.setVisibility(View.VISIBLE);
                }
                tvPmWaiqin.setVisibility(View.VISIBLE);
            } else {
                //不是外勤
                tvPmWaiqin.setVisibility(View.GONE);
                tvPmType.setVisibility(View.VISIBLE);
                if (!Utils.isNull(entity.getXiabanDakaType())) {
                    if (Utils.isNull(entity.getEndTime())) {
                        tvPmTime.setText("");
                    } else {
                        tvPmTime.setText("下班时间: " + ClockInUtils.getMinute(entity.getEndTime()));
                    }
                    tvPmType.setText(ClockInUtils.getClockInType(entity.getXiabanDakaType()));
                } else if (!Utils.isNull(entity.getXiabanBukaAuditCd())) {
                    tvPmTime.setText("下班时间: " + ClockInUtils.getMinute(entity.getBukaEndTime()));
                    tvPmType.setText(ClockInUtils.getClockInReissueCard(entity.getXiabanBukaAuditCd()));
                }
            }
            if (Utils.isNull(entity.getXiabanDakaAddress())) {
                tvPmLoc.setText("未知");
            } else {
                tvPmLoc.setText(entity.getXiabanDakaAddress());
            }
        }
        if (Utils.isNull(entity.getItemGroupName())) {
            tvAmGroup.setText("未知");
            tvPmGroup.setText("未知");
        } else {
            tvAmGroup.setText(entity.getItemGroupName() + "(" + ClockInUtils.getGroupEnd(entity.getItemGroupCode()) + ")");
            tvPmGroup.setText(entity.getItemGroupName() + "(" + ClockInUtils.getGroupEnd(entity.getItemGroupCode()) + ")");
        }
        if ("2".equals(entity.getShangbanDakaType()) || "3".equals(entity.getShangbanDakaType()) || "4".equals(entity.getShangbanDakaType())) {
            //先判断类型234异常补卡   然后根据audit判断审批状态
            if ("1".equals(entity.getShangbanYcBukaAuditCd())) {
                //补卡审核通过
                tvAmType.setTextColor(ClockInUtils.getClockInRecordColor("1"));
                tvAmType.setBackgroundResource(ClockInUtils.getClockInRecordBg("1"));
            } else {
                //补卡审核中
                tvAmType.setTextColor(ClockInUtils.getClockInRecordColor(entity.getShangbanDakaType()));
                tvAmType.setBackgroundResource(ClockInUtils.getClockInRecordBg(entity.getShangbanDakaType()));
            }
            if ("1".equals(entity.getWaiqinAuditCd())) {
                tvAmWaiqin.setTextColor(ClockInUtils.getClockInRecordColor("1"));
                tvAmWaiqin.setBackgroundResource(ClockInUtils.getClockInRecordBg("1"));
            } else {
                tvAmWaiqin.setTextColor(ClockInUtils.getClockInRecordColor("-1"));
                tvAmWaiqin.setBackgroundResource(ClockInUtils.getClockInRecordBg("-1"));
            }
        } else if ("5".equals(entity.getShangbanDakaType())) {
            //先判断类型5是否是补卡  然后根据audit判断审批状态
            if ("1".equals(entity.getShangbanBukaAuditCd())) {
                //补卡审核通过
                tvAmType.setTextColor(ClockInUtils.getClockInRecordColor("1"));
                tvAmType.setBackgroundResource(ClockInUtils.getClockInRecordBg("1"));
            } else {
                //补卡审核中
                tvAmType.setTextColor(ClockInUtils.getClockInRecordColor("-1"));
                tvAmType.setBackgroundResource(ClockInUtils.getClockInRecordBg("-1"));
            }

        } else if ("7".equals(entity.getShangbanDakaType())) {
            //先判断类型7是否是请假  然后根据audit判断审批状态
            if ("1".equals(entity.getShangbanQingjiaAuditCd())) {
                //补卡审核通过
                tvAmType.setTextColor(ClockInUtils.getClockInRecordColor("1"));
                tvAmType.setBackgroundResource(ClockInUtils.getClockInRecordBg("1"));
            } else {
                //补卡审核中
                tvAmType.setTextColor(ClockInUtils.getClockInRecordColor("-1"));
                tvAmType.setBackgroundResource(ClockInUtils.getClockInRecordBg("-1"));
            }
            //先判断waiqincd是否是外勤  然后根据audit判断审批状态
            if ("1".equals(entity.getWaiqinAuditCd())) {
                tvAmWaiqin.setTextColor(ClockInUtils.getClockInRecordColor("1"));
                tvAmWaiqin.setBackgroundResource(ClockInUtils.getClockInRecordBg("1"));
            } else {
                tvAmWaiqin.setTextColor(ClockInUtils.getClockInRecordColor("-1"));
                tvAmWaiqin.setBackgroundResource(ClockInUtils.getClockInRecordBg("-1"));
            }
        } else if ("1".equals(entity.getWaiqinCd())) {
            //先判断waiqincd是否是外勤  然后根据audit判断审批状态
            if ("1".equals(entity.getWaiqinAuditCd())) {
                tvAmWaiqin.setTextColor(ClockInUtils.getClockInRecordColor("1"));
                tvAmWaiqin.setBackgroundResource(ClockInUtils.getClockInRecordBg("1"));
                tvAmType.setTextColor(ClockInUtils.getClockInRecordColor(entity.getShangbanDakaType()));
                tvAmType.setBackgroundResource(ClockInUtils.getClockInRecordBg(entity.getShangbanDakaType()));
            } else {
                tvAmWaiqin.setTextColor(ClockInUtils.getClockInRecordColor("-1"));
                tvAmWaiqin.setBackgroundResource(ClockInUtils.getClockInRecordBg("-1"));
                tvAmType.setTextColor(ClockInUtils.getClockInRecordColor(entity.getShangbanDakaType()));
                tvAmType.setBackgroundResource(ClockInUtils.getClockInRecordBg(entity.getShangbanDakaType()));
            }
        } else {
            tvAmType.setTextColor(ClockInUtils.getClockInRecordColor(entity.getShangbanDakaType()));
            tvAmType.setBackgroundResource(ClockInUtils.getClockInRecordBg(entity.getShangbanDakaType()));
        }

        if ("2".equals(entity.getXiabanDakaType()) || "3".equals(entity.getXiabanDakaType()) || "4".equals(entity.getXiabanDakaType())) {
            //先判断类型234异常补卡   然后根据audit判断审批状态
            if ("1".equals(entity.getXiabanYcBukaAuditCd())) {
                //补卡审核通过
                tvPmType.setTextColor(ClockInUtils.getClockInRecordColor("1"));
                tvPmType.setBackgroundResource(ClockInUtils.getClockInRecordBg("1"));
            } else {
                //补卡审核中
                tvPmType.setTextColor(ClockInUtils.getClockInRecordColor(entity.getXiabanDakaType()));
                tvPmType.setBackgroundResource(ClockInUtils.getClockInRecordBg(entity.getXiabanDakaType()));
            }
            if ("1".equals(entity.getXiabanWaiqinAuditCd())) {
                tvPmWaiqin.setTextColor(ClockInUtils.getClockInRecordColor("1"));
                tvPmWaiqin.setBackgroundResource(ClockInUtils.getClockInRecordBg("1"));
            } else {
                tvPmWaiqin.setTextColor(ClockInUtils.getClockInRecordColor("-1"));
                tvPmWaiqin.setBackgroundResource(ClockInUtils.getClockInRecordBg("-1"));
            }
        } else if ("5".equals(entity.getXiabanDakaType())) {
            if ("1".equals(entity.getXiabanBukaAuditCd())) {
                //补卡审核通过
                tvPmType.setTextColor(ClockInUtils.getClockInRecordColor("1"));
                tvPmType.setBackgroundResource(ClockInUtils.getClockInRecordBg("1"));
            } else {
                //补卡审核中
                tvPmType.setTextColor(ClockInUtils.getClockInRecordColor("-1"));
                tvPmType.setBackgroundResource(ClockInUtils.getClockInRecordBg("-1"));
            }
        } else if ("7".equals(entity.getXiabanDakaType())) {
            if ("1".equals(entity.getXiabanQingjiaAuditCd())) {
                //补卡审核通过
                tvPmType.setTextColor(ClockInUtils.getClockInRecordColor("1"));
                tvPmType.setBackgroundResource(ClockInUtils.getClockInRecordBg("1"));
            } else {
                //补卡审核中
                tvPmType.setTextColor(ClockInUtils.getClockInRecordColor("-1"));
                tvPmType.setBackgroundResource(ClockInUtils.getClockInRecordBg("-1"));
            }
            if ("1".equals(entity.getXiabanWaiqinAuditCd())) {
                tvPmWaiqin.setTextColor(ClockInUtils.getClockInRecordColor("1"));
                tvPmWaiqin.setBackgroundResource(ClockInUtils.getClockInRecordBg("1"));
            } else {
                tvPmWaiqin.setTextColor(ClockInUtils.getClockInRecordColor("-1"));
                tvPmWaiqin.setBackgroundResource(ClockInUtils.getClockInRecordBg("-1"));
            }
        } else if ("1".equals(entity.getXiabanWaiqinCd())) {
            if ("1".equals(entity.getXiabanWaiqinAuditCd())) {
                tvPmWaiqin.setTextColor(ClockInUtils.getClockInRecordColor("1"));
                tvPmWaiqin.setBackgroundResource(ClockInUtils.getClockInRecordBg("1"));
                tvPmType.setTextColor(ClockInUtils.getClockInRecordColor(entity.getXiabanDakaType()));
                tvPmType.setBackgroundResource(ClockInUtils.getClockInRecordBg(entity.getXiabanDakaType()));
            } else {
                tvPmWaiqin.setTextColor(ClockInUtils.getClockInRecordColor("-1"));
                tvPmWaiqin.setBackgroundResource(ClockInUtils.getClockInRecordBg("-1"));
                tvPmType.setTextColor(ClockInUtils.getClockInRecordColor(entity.getXiabanDakaType()));
                tvPmType.setBackgroundResource(ClockInUtils.getClockInRecordBg(entity.getXiabanDakaType()));
            }
        } else {
            tvPmType.setTextColor(ClockInUtils.getClockInRecordColor(entity.getXiabanDakaType()));
            tvPmType.setBackgroundResource(ClockInUtils.getClockInRecordBg(entity.getXiabanDakaType()));
        }
        tvAmWaiqin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (Urls.antiShake.check()) {
                    return;
                }
                Intent intent = new Intent(context, ClockInReissueCardDetailActivity.class);
                intent.putExtra("clockInRecordEntity", entity);
                intent.putExtra("shangbanCd", 1);
                intent.putExtra("isRecord", true);
                intent.putExtra("type", 5);
                context.startActivity(intent);
            }
        });
        tvPmWaiqin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (Urls.antiShake.check()) {
                    return;
                }
                Intent intent = new Intent(context, ClockInReissueCardDetailActivity.class);
                intent.putExtra("clockInRecordEntity", entity);
                intent.putExtra("shangbanCd", 0);
                intent.putExtra("isRecord", true);
                intent.putExtra("type", 5);
                context.startActivity(intent);
            }
        });
        tvAmType.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (Urls.antiShake.check()) {
                    return;
                }
                if (Utils.isNull(entity.getShangbanDakaType())) {
                    //上班数据为空，且没有进行补卡
                    if (Utils.isNull(entity.getShangbanBukaAuditCd())) {
                        //未补卡
                        if (!Utils.isNull(groupCode)) {
                            return;
                        }
                        Intent intent = new Intent(context, ClockInReissueCardActivity.class);
                        intent.putExtra("data", entity);
                        intent.putExtra("type", 1);
                        context.startActivity(intent);
                    } else {
                        //补卡中
                        Intent intent = new Intent(context, ClockInReissueCardDetailActivity.class);
                        intent.putExtra("clockInRecordEntity", entity);
                        intent.putExtra("shangbanCd", 1);
                        intent.putExtra("isRecord", true);
                        if ("1".equals(entity.getWaiqinCd())) {
                            intent.putExtra("type", 5);
                        }
                        context.startActivity(intent);
                    }
                } else {
                    if ("2".equals(entity.getShangbanDakaType()) || "3".equals(entity.getShangbanDakaType()) || "4".equals(entity.getShangbanDakaType())) {
                        //迟到 早退 旷工

                        if (Utils.isNull(entity.getShangbanYcBukaAuditCd())) {
                            //去补卡
                            if (!Utils.isNull(groupCode)) {
                                return;
                            }
                            Intent intent = new Intent(context, ClockInReissueCardActivity.class);
                            intent.putExtra("data", entity);
                            intent.putExtra("type", 1);
                            context.startActivity(intent);
                        } else {
                            //补卡成功 或补卡中
                            Intent intent = new Intent(context, ClockInReissueCardDetailActivity.class);
                            intent.putExtra("clockInRecordEntity", entity);
                            intent.putExtra("shangbanCd", 1);
                            intent.putExtra("isRecord", true);
                            context.startActivity(intent);
                        }
                    } else if ("1".equals(entity.getShangbanBukaAuditCd()) || "5".equals(entity.getShangbanDakaType())) {
//                            1-正常 2-迟到 3-旷工 4-早退 5-补卡6请假
                        Intent intent = new Intent(context, ClockInReissueCardDetailActivity.class);
                        intent.putExtra("clockInRecordEntity", entity);
                        intent.putExtra("shangbanCd", 1);
                        intent.putExtra("isRecord", true);
                        if ("1".equals(entity.getWaiqinCd())) {
                            intent.putExtra("type", 5);
                        }
                        context.startActivity(intent);
                    } else if ("7".equals(entity.getShangbanDakaType())) {
                        Intent intent = new Intent(context, ClockInAskHolidayDetailActivity.class);
                        intent.putExtra("projectCode", entity.getShangbanQingjiaWorkflowCode());
                        intent.putExtra("employeeCode", entity.getEmployeeName());
                        intent.putExtra("title", entity.getEmployeeName() + "的请假申请");
                        context.startActivity(intent);
                    }
                }
            }
        });
        tvPmType.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (Urls.antiShake.check()) {
                    return;
                }
                if (Utils.isNull(entity.getXiabanDakaType())) {
                    //下班数据为空，且没有进行补卡
                    if (Utils.isNull(entity.getXiabanBukaAuditCd())) {
                        //未补卡
                        if (!Utils.isNull(groupCode)) {
                            return;
                        }
                        Intent intent = new Intent(context, ClockInReissueCardActivity.class);
                        intent.putExtra("data", entity);
                        intent.putExtra("type", 0);
                        context.startActivity(intent);
                    } else {
                        //补卡中
                        Intent intent = new Intent(context, ClockInReissueCardDetailActivity.class);
                        intent.putExtra("clockInRecordEntity", entity);
                        intent.putExtra("shangbanCd", 0);
                        intent.putExtra("isRecord", true);
                        if ("1".equals(entity.getXiabanWaiqinCd())) {
                            intent.putExtra("type", 5);
                        }
                        context.startActivity(intent);
                    }
                } else {
                    if ("2".equals(entity.getXiabanDakaType()) || "3".equals(entity.getXiabanDakaType()) || "4".equals(entity.getXiabanDakaType())) {
                        //迟到 早退 旷工
                        if (Utils.isNull(entity.getXiabanYcBukaAuditCd())) {
                            //去补卡
                            if (!Utils.isNull(groupCode)) {
                                return;
                            }
                            Intent intent = new Intent(context, ClockInReissueCardActivity.class);
                            intent.putExtra("data", entity);
                            intent.putExtra("type", 0);
                            context.startActivity(intent);
                        } else {
                            //补卡成功 或补卡中
                            Intent intent = new Intent(context, ClockInReissueCardDetailActivity.class);
                            intent.putExtra("clockInRecordEntity", entity);
                            intent.putExtra("shangbanCd", 0);
                            intent.putExtra("isRecord", true);
                            context.startActivity(intent);
                        }
                    } else if ("1".equals(entity.getXiabanBukaAuditCd()) || "5".equals(entity.getXiabanDakaType())) {
                        //补卡成功
                        Intent intent = new Intent(context, ClockInReissueCardDetailActivity.class);
                        intent.putExtra("clockInRecordEntity", entity);
                        intent.putExtra("shangbanCd", 0);
                        intent.putExtra("isRecord", true);
                        if ("1".equals(entity.getXiabanWaiqinCd())) {
                            intent.putExtra("type", 5);
                        }
                        context.startActivity(intent);
                    } else if ("7".equals(entity.getXiabanDakaType())) {
                        Intent intent = new Intent(context, ClockInAskHolidayDetailActivity.class);
                        intent.putExtra("projectCode", entity.getXiabanQingjiaWorkflowCode());
                        intent.putExtra("employeeCode", entity.getEmployeeName());
                        intent.putExtra("title", entity.getEmployeeName() + "的请假申请");
                        context.startActivity(intent);
                    }
                }
            }
        });
    }
}
